package com.glory.wxelectricitypda.activity;


import android.os.Handler;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.LockersOnTheShelvesAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.LockersOnTheShelvesBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class LockersOnTheShelvesActivity extends BaseDialogActivity {

    @BindView(R.id.llMain)
    LinearLayout llMain;
//    @BindView(R.id.useID)
//    FreeUI_ClearEditText useID;
    @BindView(R.id.mlotID)
    FreeUI_ClearEditText mlotID;
//    @BindView(R.id.eqpID)
//    FreeUI_ClearEditText eqpID;
    @BindView(R.id.storageID)
    TextView storageID;
    @BindView(R.id.mlotRecycler)
    RecyclerView mlotRecycler;

    private LockersOnTheShelvesAdapter adapter;
    private List<LockersOnTheShelvesBean.MLOTLISTDTO> mlotlist = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_lockers_on_the_shelves;
    }

    @Override
    protected void initView() {

        freeUI_titleView.setTitle("储物柜上架");
        freeUI_titleView.setRightListener("上架", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (useID.getText().toString().isEmpty()){
//                    showToast("请扫描工号！",false);
//                    return;
//                }
                if (mlotID.getText().toString().isEmpty()){
                    showToast("请扫描物料批号！",false);
                    return;
                }
//                if (eqpID.getText().toString().isEmpty()){
//                    showToast("请扫描库位号！",false);
//                    return;
//                }

                queryEqpID();
//                submit();
            }
        });

        mlotRecycler.setLayoutManager(new LinearLayoutManager(LockersOnTheShelvesActivity.this));
        mlotRecycler.setItemAnimator(new DefaultItemAnimator());
        mlotRecycler.addItemDecoration(new DividerItemDecoration(LockersOnTheShelvesActivity.this, 1));
        adapter = new LockersOnTheShelvesAdapter(null);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.isFirstOnly(true);
        mlotRecycler.setAdapter(adapter);

    }

    private void submit(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        JSONObject jsonObject01 = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        try {
            map.put("TOWAREHOUSEID", "RAW_STOCK");
            map.put("TOSTORAGEID", storageID.getText().toString().trim());
            map.put("TOSTORAGETYPE", "receive");
            map.put("ACTIONCODE", "A");
            jsonObject01.put("MLOTID",mlotID.getText().toString());
            jsonArray.put(jsonObject01);
            map.put("MLOTLIST", jsonArray);
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.EuitTransferMLot");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<LockersOnTheShelvesBean>>(
                new TypeToken<ResponseBean<LockersOnTheShelvesBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LockersOnTheShelvesBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {

                }
            }

            @Override
            public void onError(Response<ResponseBean<LockersOnTheShelvesBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EUIT.EuitTransferMLot");
            }
        });

    }


    @Override
    protected void initData() {

        initEdit(mlotID,s->{
//            if (useID.getText().toString().isEmpty()){
//                showToast("请扫描工号！",false);
//                mlotID.setText("");
//                return;
//            }
//            if (eqpID.getText().toString().isEmpty()){
//                showToast("请扫描库房号！",false);
//                mlotID.setText("");
//                return;
//            }
            if (s.contains("|")) {
                s = s.replace("|", ",").split(",")[2];
            }
            mlotID.setText(s);
        });

    }

    private void  queryEqpID(){

        JSONObject map = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        try {
            map.put("ACTIONCODE", "onShelfQueryStorage");
//            map.put("EQPID", eqpID.getText().toString());
//            map.put("JOBID",useID.getText().toString());
            map.put("MLOTID",mlotID.getText().toString());
            jsonObject.put("Data", map);
            jsonObject.put("MessageName", "EUIT.EuitTransferMLot");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.requestPostMesForwardUtils(jsonObject, this, new FreeUI_DialogEntityCallBack<ResponseBean<LockersOnTheShelvesBean>>(
                new TypeToken<ResponseBean<LockersOnTheShelvesBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<ResponseBean<LockersOnTheShelvesBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    showToast("上架成功", true);
                    response.body().getBody().getMLOTLIST().get(0).settowarehouseId(response.body().getBody().getSTORAGEID());
                    mlotlist.add(response.body().getBody().getMLOTLIST().get(0));
                    adapter.setNewData(mlotlist);
                    mlotID.setText("");
//                    storageID.setText("");
                    new Handler().postDelayed(() -> {
                        mlotID.requestFocus();
                    }, 500);
                }
            }

            @Override
            public void onError(Response<ResponseBean<LockersOnTheShelvesBean>> response) {
                super.onError(response);
                loadError(response.getException(), "EuitTransferMLot");
            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity("header",llMain);
    }
}